package com.lfm.homework;

import org.junit.Test;

import java.util.*;

public class Homework01 {
    public static void main(String[] args) {
        //
    }
    @Test
    public void testList() {

        //这里我们给 T指定类型是 User
        DAO<User> dao = new DAO<>();
        dao.save("001", new User(1, 10, "jack"));
        dao.save("002", new User(2, 18, "king"));
        dao.save("003", new User(3, 38, "smith"));

        List<User> list = dao.list();

        System.out.println(list);

        dao.update("003", new User(3, 59, "ml"));
        System.out.println(list);
    }
}

class DAO<T> {
    private HashMap<String, T> map = new HashMap<>();

    public void save(String id, T entity) {
        map.put(id, entity);;
    }
    public T get(String id) {
        return map.get(id);
    }
    public void update(String id, T entity) {
        map.put(id, entity);
    }
    public List<T> list() {
        List<T> list = new ArrayList<>();
        Set<Map.Entry<String, T>> set = map.entrySet();
        for (Map.Entry<String, T> entry : set) {
            list.add(entry.getValue());
        }
        return list;
    }
    public void delete(String id) {
        map.remove(id);
    }

}


class User{
    private int id;
    private int age;
    String name;

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}